package com.abso.sunlight.explorer.handlers;

import java.util.List;

import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.jface.wizard.Wizard;
import org.eclipse.jface.wizard.WizardDialog;
import org.eclipse.ui.handlers.HandlerUtil;

import com.abso.sunlight.api.Legislator;
import com.abso.sunlight.explorer.editors.LegislatorListEditor;
import com.abso.sunlight.explorer.wizards.ExportExcelWizard;

/**
 * Export a list of legislators to a Excel spreadsheet.
 */
public class ExportLegislatorsAsExcelHandler extends AbstractHandler {

    /* (non-Javadoc)
     * @see org.eclipse.core.commands.AbstractHandler#execute(org.eclipse.core.commands.ExecutionEvent)
     */
    public Object execute(ExecutionEvent event) throws ExecutionException {
        LegislatorListEditor editor = (LegislatorListEditor) HandlerUtil.getActiveEditor(event);

        /* opens the export wizard */
        List<Legislator> sortedLegislators = editor.getSortedLegislators();
        List<Legislator> sortedSelectedLegislators = editor.getSortedSelectedLegislators();
        Wizard wizard = new ExportExcelWizard(sortedLegislators, sortedSelectedLegislators, sortedSelectedLegislators.size() <= 1);
        WizardDialog dialog = new WizardDialog(HandlerUtil.getActiveShell(event), wizard);
        dialog.open();
        return null;
    }

}
